package com.stylefeng.guns.modular.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.modular.system.dao.CmsUserDemandMapper;
import com.stylefeng.guns.modular.system.model.CmsAllAuthInfo;
import com.stylefeng.guns.modular.system.model.CmsAuthInfo;
import com.stylefeng.guns.modular.system.dao.CmsAuthInfoMapper;
import com.stylefeng.guns.modular.system.model.CmsUserDemand;
import com.stylefeng.guns.modular.system.service.ICmsAuthInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-10-22
 */
@Service
public class CmsAuthInfoServiceImpl extends ServiceImpl<CmsAuthInfoMapper, CmsAuthInfo> implements ICmsAuthInfoService {
    @Autowired
    private  CmsAuthInfoMapper cmsAuthInfoMapper;
    @Autowired
    private CmsUserDemandMapper cmsUserDemandMapper;

    @Override
    public List<CmsAllAuthInfo> getAllInfo(Map<String,Object> params) {
        return cmsAuthInfoMapper.selectAuthList(params);
    }

    @Override
    public Map<String,Object> getInfoByUserId(Long userId) {
        Map<String,Object> map = cmsAuthInfoMapper.selectInfoByUserId(userId);
        return map;
    }

    @Override
    public void updateCmsAuthInfo(CmsAllAuthInfo cmsAllAuthInfo) {
       
        Long uId=Integer.valueOf(cmsAllAuthInfo.getId()).longValue();
        Map<String,Object> demandMap =cmsUserDemandMapper.getDemandInfoByDetail(cmsAllAuthInfo.getDemandDetail(),uId);
        
        CmsUserDemand detail = JSON.parseObject(JSON.toJSONString(demandMap),  CmsUserDemand.class);
            if(cmsAllAuthInfo.getDemandDetail()!=null&cmsAllAuthInfo.getDemandDetail()!=""){
               
                if(detail==null){
                    CmsUserDemand cmsUserDemand=new CmsUserDemand();
                    cmsUserDemand.setUserId(uId);
                    cmsUserDemand.setDemandDetail(cmsAllAuthInfo.getDemandDetail());
                    cmsUserDemand.setCreateTime(new Date());
                    cmsUserDemand.setUpdateTime(new Date());
                    cmsUserDemandMapper.insert(cmsUserDemand);
                }
               
            }
           
            
        
        if(cmsAllAuthInfo!=null){

            //获取贷款金额详情
            Map<String,Object>map =cmsUserDemandMapper.selectByUserId(uId);
            CmsUserDemand demand = JSON.parseObject(JSON.toJSONString(map),CmsUserDemand.class);
            
            CmsAuthInfo cmsAuthInfo=new CmsAuthInfo();
            cmsAuthInfo.setConnectStatus(cmsAllAuthInfo.getConnectStatus());
            cmsAuthInfo.setHandleStatus(cmsAllAuthInfo.getHandleStatus());
            cmsAuthInfo.setCreditCardStatus(cmsAllAuthInfo.getCreditCardStatus());
            cmsAuthInfo.setHouseStatus(cmsAllAuthInfo.getHouseStatus());
            cmsAuthInfo.setLegalPersonStatus(cmsAllAuthInfo.getLegalPersonStatus());
            cmsAuthInfo.setLoanAmount(cmsAllAuthInfo.getLoanAmount());
            cmsAuthInfo.setLoanStatus(cmsAllAuthInfo.getLoanStatus());
            cmsAuthInfo.setPolicyStatus(cmsAllAuthInfo.getPolicyStatus());
            cmsAuthInfo.setRecommendStatis(cmsAllAuthInfo.getRecommendStatis());
            cmsAuthInfo.setRemark(cmsAllAuthInfo.getRemark());
            cmsAuthInfo.setReserve1Status(cmsAllAuthInfo.getReserve1Status());
            cmsAuthInfo.setReserve2Status(cmsAllAuthInfo.getReserve2Status());
            cmsAuthInfo.setWeiliLoanStatus(cmsAllAuthInfo.getWeiliLoanStatus());
            cmsAuthInfo.setUserId(uId);
            if(cmsAllAuthInfo.getCmsAuthInfoId()==null){
                if(StringUtils.isNoneBlank(cmsAllAuthInfo.getDemandDetail())){
                    if(detail==null){
                        if(demand!=null){
                            cmsAuthInfo.setDemandId(demand.getId());
                        }
                       
                    }else{
                        cmsAuthInfo.setDemandId(detail.getId());
                    }
                    cmsAuthInfo.setCreateTime(new Date());
                    cmsAuthInfo.setUpdateTime(new Date());
                    cmsAuthInfoMapper.insert(cmsAuthInfo);
                }
               
               


            }else {
                if(cmsAllAuthInfo.getDemandDetail()!=null&cmsAllAuthInfo.getDemandDetail()!=""){
                    if(detail==null){
                        cmsAuthInfo.setDemandId(demand.getId());
                    }else {
                        cmsAuthInfo.setDemandId(detail.getId());
                    }
                    //获取当前的认证信息
                    CmsAuthInfo info=cmsAuthInfoMapper.selectById(cmsAllAuthInfo.getCmsAuthInfoId());
                    cmsAuthInfo.setId(cmsAllAuthInfo.getCmsAuthInfoId());

                    cmsAuthInfo.setCreateTime(info.getCreateTime());
                    cmsAuthInfo.setUpdateTime(new Date());
                    cmsAuthInfoMapper.updateAllColumnById(cmsAuthInfo);
                }
               

            }


        }
       
       
       
        
    }

}
